<?php namespace App\Controllers;

use CodeIgniter\Controller;
use \App\Models\UserModel;
use CodeIgniter\I18n\Time;


class Login  extends BaseController
{
	protected $regex= '/[^A-Za-z0-9!?\s]+/';
	//above should clean everything except numbers, letters white space
	protected $name;
	protected $password;
	protected $captchaEntered;
	protected $actualCaptcha;
	protected $passwordMatches;
	protected $hashPassword;
	protected $userNameDB;
	protected $userNameMatches;
	protected $captchaMatches;
    protected $myTime;
protected $myDate;
				 public function __construct() 
				    {
						parent::__construct();
						$this->myTime = parent::getTime();
						$this->myDate= date("d/m/Y",$this->myTime); 	
					
			       }
 












		public function index()

		{
		echo "heelo from login";

		}

		public function login()
		{
		session_start();
		 $myarray = array( chr(rand(48,57)) , chr(rand(48,57)) , chr(rand(48,57)) , chr(rand(48,57)) , chr(rand(48,57)));
		$_SESSION['captcha']= $myarray;			
                $data = [
				'title'  => 'login  page',
				 'captcha'=>$myarray,
				 'date'=>$this->myDate
	        
			];

		
		echo view('usrLogin',$data);
		
	
											
											
		}											

public function credentials()

{
	session_start();
	//maybe 	should use removeallbad chars here 
	$theArray= $_SESSION['captcha'];
	$this->actualCaptcha= implode("",$theArray);
    $name = $this->request->getVar('user'); 
	$name2 = htmlentities($name,ENT_QUOTES);
	$this->name= preg_replace($this->regex,"",$name2);
	$this->name= trim($this->name);
	
	$password= $this->request->getVar('userPassword');
	$password2= htmlentities($password,ENT_QUOTES);
	$this->password= preg_replace($this->regex,"",$password2);
	$this->password= trim($this->password);
	
	$captcha= $this->request->getVar('captcha'); 
	$captcha2= htmlentities($captcha,ENT_QUOTES);
	$this->captcha= preg_replace($this->regex,"",$captcha2);
	$this->captcha= trim($this->captcha);
	$handle= new UserModel();

		try {
		   $result2= $handle->getOne('admin');      
		   $this->hashPassword=  $result2['Password'];
		   $this->userNameDB=$result2['Name'];


		   }
		catch (\Exception $e)
		{
		      echo ($e->getMessage());
		      //can do redirect here etc
		$data= [
		
		'title'=>'problem',
		
		'info'=>$e->getMessage(),
		'date'=>$myDate
		
		];
		
		  
        echo view('info', $data);
                
	
		
		
		
		}






		$this->passwordMatches= password_verify($this->password,$this->hashPassword);
		$this->userNameMatches= strcmp($this->userNameDB, $this->name);
		//if true value will be 0
		$this->captchaMatches=strcmp($this->captcha,$this->actualCaptcha);


		if  (  ($this->userNameMatches ==0) && ($this->captchaMatches ==0) &&($this->passwordMatches == True)   )
		{

			
			$_SESSION['role']="admin";
			
			$data= 
			
			
			[
			'title'=>'info',
			'info'=>'<p>
			
			<h11>Hello</h11> admin your now logged in 
			</p> ' ,
			'date'=>$this->myDate,
		
			
			];
			
		
        echo view('admin', $data);
       
			
			 

		}

		else
		{
                
               $logic =isset(   $_SESSION['count']);
               if ($logic ==false)
               {
				$_SESSION['count']=5;   
				return redirect()->route('blackcat');
				
				
				
				}   
				   
                elseif($logic==true)
                
                {
				if(	$_SESSION['count']<=1 )
				{
					
					return redirect()->to('http://www.google.com'); 
					
			   }		
					
			    else
			    
			    {
					          
					                   $oldCount=  $_SESSION['count'];
										$newCount = $oldCount-1;
										$_SESSION['count']= $newCount;      
					
					
					
					
					return redirect()->route('blackcat');
					
					
				}
			    
			    
			    
			    
			    }		
               
			

		}
              }
               public function logout()
              {
               session_start();
               
	 
               
               
                unset($_SESSION['role']);
                unset($_SESSION['count']);
              
	      $data= [ 
	      'title'=> 'logout',
	      'info'=> 'you may have already been logged out, but if you were not you are now !' ,
	      'date'=>$this->myDate
	      
	      
	      
	      ];
	      
	      
	      
        echo view('info', $data);
        
	
            
	    
	    
	    
	    
	    
	    
	     }


		public function admin()
		{
		 
	 
		
		
		
		 $data= [ 
				  'title'=> 'admin',
				  'info'=> 'your logged in dont forget to logout' ,
				  'date'=>$this->myDate
				  
				  
				  
				  ];
				  
				  
				  
				echo view('admin', $data);
				
		}
}
  
